#include "sys.h"
#include "log.h"

int Debug_mode(){
    if(access("./wd_debug", F_OK) == 0){
        return 1;
    }else{
        return 0;
    }

}

void system_killall(char *exec){
    char cmd[256] = {0};

    sprintf(cmd, "killall -9 %s", exec);
    if(0 != system(cmd)){
#ifdef __ANDROID__
        bzero(cmd, sizeof(cmd));
        sprintf(cmd, "busybox killall -9 %s", exec);
        if(0 != system(cmd)){
            log_error("CMD killall exeuted failed !!!");
        }
#endif
    }
}

void get_timestring(char* buf)
{
    struct tm *p, tm_p;
    time_t timep;

    time(&timep);
    localtime_r(&timep, &tm_p);
    p = &tm_p;
    sprintf(buf, "%d-%02d-%02d %02d:%02d:%02d",
        1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
}


void sys_time_update(uint32_t sec, uint32_t minuteswest){
    int diff_tm;
    struct timeval remote_now = {0};
    struct timezone local_zn = {0};

    remote_now.tv_sec = sec;
    remote_now.tv_usec = 0;
    local_zn.tz_minuteswest = minuteswest;
    local_zn.tz_dsttime = 0;

    if(0 > settimeofday(&remote_now, &local_zn)){
        log_syserr("settimeofday error.");
    }

}
